package com.tony.mindhr.service;

import com.tony.mindhr.mapper.EmployeeMapper;
import com.tony.mindhr.model.Employee;
import com.tony.mindhr.model.RespPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: EmployeeService
 * @Auther: 皮蛋布丁
 * @Date: 2021/06/13/20:30
 * @Description:
 */
@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;
    SimpleDateFormat yearFormat =  new SimpleDateFormat("yyyy");
    SimpleDateFormat monFormat =  new SimpleDateFormat("MM");
    DecimalFormat decimalFormat = new DecimalFormat("##.00");

    /**
    * @Description: getEmployeeByPage 获取员工列表信息(分页)
    * @Param: [page, size]
    * @return: com.tony.mindhr.model.RespPageBean
    * @Author: 皮蛋布丁
    * @Date: 2021/6/13 20:37
    */
    public RespPageBean getEmployeeByPage(Integer page, Integer size, Employee employee,Date[] beginDateScope) {
        if (page != null && size != null) {
            page = (page -1)*size;
        }
        List<Employee> data = employeeMapper.getEmployeeByPage(page,size,employee,beginDateScope);
        Long total = employeeMapper.getTotal(employee,beginDateScope);
        RespPageBean bean = new RespPageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }

    /**
    * @Description: addEmp 添加员工信息
    * @Param: [employee]
    * @return: java.lang.Integer
    * @Author: 皮蛋布丁
    * @Date: 2021/6/14 16:13
    */
    public Integer addEmp(Employee employee) {
        if (employee.getWorkState() != null) {
            employee.getWorkState().trim();
        }
        Date beginContract = employee.getBeginContract();
        Date endContract = employee.getEndContract();
        double month = (Double.parseDouble(yearFormat.format(endContract)) - Double.parseDouble(yearFormat.format(beginContract)))*12 + (Double.parseDouble(monFormat.format(endContract)) - Double.parseDouble(monFormat.format(beginContract)));
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(month / 12)));
        return employeeMapper.insertSelective(employee);
    }

    /**
    * @Description: maxworkID 获取最大工号
    * @Param: []
    * @return: java.lang.Integer
    * @Author: 皮蛋布丁
    * @Date: 2021/6/14 21:08
    */
    public Integer maxworkID() {
        return employeeMapper.maxworkID();
    }

    /**
    * @Description: deleteEmpById 根据id删除员工
    * @Param: [id]
    * @return: java.lang.Integer
    * @Author: 皮蛋布丁
    * @Date: 2021/6/15 21:56
    */
    public Integer deleteEmpById(Integer id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }

    /**
    * @Description: updateEmp 更新员工
    * @Param: [employee]
    * @return: java.lang.Integer
    * @Author: 皮蛋布丁
    * @Date: 2021/6/16 20:41
    */
    public Integer updateEmp(Employee employee) {
        return employeeMapper.updateByPrimaryKeySelective(employee);
    }

    /**
    * @Description: addEmps 插入list员工数据
    * @Param: [list]
    * @return: java.lang.Integer
    * @Author: 皮蛋布丁
    * @Date: 2021/6/19 20:50
    */
    public Integer addEmps(List<Employee> list) {
        return employeeMapper.addEmps(list);
    }
}
